package com.xinke.edu.mypetadoption.fragment2;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.xinke.edu.mypetadoption.databinding.FragmentTjBinding;
import com.xinke.edu.mypetadoption.entity.OrdersUserProductDTO;
import com.xinke.edu.mypetadoption.entity.ResultBody;
import com.xinke.edu.mypetadoption.util.RetrofitUtil;
import com.xinke.edu.mypetadoption.util.SPUtils;

import java.math.BigDecimal;
import java.util.List;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class TjFragment extends Fragment {


    FragmentTjBinding binding;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        binding = FragmentTjBinding.inflate(inflater, container, false);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        loaddata();

    }

    private void loaddata(){

        //获取登录用户id（确保与Login_Activity中存储的ID完全一致）
        int userId = SPUtils.getInstance(requireContext()).getInt("userId", 1);
        
        // 添加日志输出，便于调试验证ID值
        // Log.d("TjFragment", "使用的用户ID: " + userId);
        
        RetrofitUtil.getApi().selectOrdersMerchant(userId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResultBody<List<OrdersUserProductDTO>>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {}

                    @Override
                    public void onNext(@NonNull ResultBody<List<OrdersUserProductDTO>> result) {

                        List<OrdersUserProductDTO> list = result.getData();
                        if (list != null && !list.isEmpty()) {
                            // 1️⃣ 计算总金额
                            BigDecimal totalSum = BigDecimal.ZERO;
                            for (OrdersUserProductDTO order : list) {
                                if (order.getTotalAmount() != null) {
                                    totalSum = totalSum.add(order.getTotalAmount());
                                }
                            }

                            // 2️⃣ 更新 TextView
                            binding.xsze.setText("¥" + totalSum.toPlainString());
                            binding.dds.setText(String.valueOf(list.size()));
                            binding.xzkh.setText(String.valueOf(list.size()));
                            binding.rxmc.setText(list.get(0).getProductName());
                            binding.rxxl.setText("销量：" + (list.get(0).getQuantity() * 6) + "件");
                            binding.rxse.setText("¥" + list.get(0).getProductPrice().toPlainString());




                        } else {
                            binding.xsze.setText("¥0");
                            binding.dds.setText("0");
                            binding.xzkh.setText("0");
                        }

                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Toast.makeText(requireContext(), "加载失败：" + e.getMessage(), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onComplete() {}
                });

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

}
